package com.example.lib_myrouter;

import android.app.Activity;
import android.content.Intent;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/23
 */
public class Router {
    //静态内部类单例
    public static Router getInstance(){
        return Holder.instance;
    }
    private static class Holder{
        private static final Router instance = new Router();
    }

    private Map<String, Map<String,Class<?>>> groupMap = new HashMap<>();


    /**
     *
     * @param path  /main/SplashActivity
     * @param clazz
     */
    public void register( String path, Class<?> clazz){
        String[] split = path.split("/");
        if(split.length > 2){
            String groupName = split[1];
            Map<String,Class<?>> group = null;
            if(groupMap.containsKey(groupName)){
                group = groupMap.get(groupName);
                group.put(path,clazz);
            }
            if(group == null){
                group = new HashMap<>();
                group.put(path,clazz);
                groupMap.put(groupName,group);
            }
        }
    }

    /**
     * 导航
     * @param activity
     * @param path
     */
    public void navigation(Activity activity, String path){
        String[] split = path.split("/");
        if(split.length > 2){
            String groupName = split[1];
            Map<String,Class<?>> group = null;
            if(groupMap.containsKey(groupName)){
                group = groupMap.get(groupName);
            }
            if(group != null){
                Class<?> clazz_ = group.get(path);
                activity.startActivity(new Intent(activity,clazz_));
            }

        }
    }




}
